{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.keras import optimizers\n",
    "from tensorflow.keras import losses\n",
    "from tensorflow.keras.callbacks import EarlyStopping\n",
    "net.compile(optimizer=optimizers.Adam(lr=0.001),\n",
    "        loss = losses.CategoricalCrossentropy(from_logits=True),\n",
    "        metrics=[\"accuracy\"])\n",
    "callbacks = [EarlyStopping(monitor=\"val_loss\",min_delta=0.001,patience=5)]\n",
    "net.fit(db_train, \n",
    "      validation_data=db_valid,\n",
    "      validation_freq=1,\n",
    "      epochs=50,\n",
    "      callbacks=callbacks)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.evaluate(db_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.save_weights(\"net_weights.h5\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.load_weights(\"net_weights.h5\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Model: \"sequential\"\n_________________________________________________________________\nLayer (type)                 Output Shape              Param #   \n=================================================================\nvgg19 (Model)                (None, 512)               20024384  \n_________________________________________________________________\ndense (Dense)                (None, 4)                 2052      \n=================================================================\nTotal params: 20,026,436\nTrainable params: 2,052\nNon-trainable params: 20,024,384\n_________________________________________________________________\n"
    }
   ],
   "source": [
    "from tensorflow import keras \n",
    "from tensorflow.keras.layers import Dense\n",
    "from tensorflow.keras.models import Sequential\n",
    "net = keras.applications.VGG19(weights=\"imagenet\",include_top=False,pooling=\"max\")\n",
    "net.trainable=False\n",
    "newnet = Sequential([\n",
    "            net,\n",
    "            Dense(4)\n",
    "])\n",
    "newnet.build(input_shape=(None,64,64,3))\n",
    "newnet.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#newnet是识别4个手势的权重\n",
    "newnet.load_weights(r\"E:\\pycharm\\Graduated_project\\Again\\newnet.h5\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n2\n2\n0\n0\n2\n2\n0\n2\n1\n3\n3\n3\n3\n1\n1\n1\n1\n1\n2\n3\n2\n2\n2\n2\n0\n"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import cv2\n",
    "import os\n",
    "import tensorflow as tf\n",
    "start = r\"E:\\pycharm\\Graduated_project\\Again\\start_icon.png\"\n",
    "call_path = r\"E:\\pycharm\\Graduated_project\\Again\\images\\call-me-hand_1f919.png\"\n",
    "ok_path = r\"E:\\pycharm\\Graduated_project\\Again\\images\\ok-hand-sign_1f44c.png\"\n",
    "thumbsdown = r\"E:\\pycharm\\Graduated_project\\Again\\images\\thumbs-down-sign_1f44e.png\"\n",
    "thumbsup = r\"E:\\pycharm\\Graduated_project\\Again\\images\\thumbs-up-sign_1f44d.png\"\n",
    "victory = r\"E:\\pycharm\\Graduated_project\\Again\\images\\victory-hand_270c.png\"\n",
    "wave = r\"E:\\pycharm\\Graduated_project\\Again\\images\\waving-hand-sign_1f44b.png\"\n",
    "labels = [\"call_me\",\"ok\",\"thumbsup\",\"victory\",\"wave\"]\n",
    "labels_path = [call_path,ok_path,thumbsup,victory,wave]\n",
    "#窗口定义\n",
    "ptLeftTop = (50, 50)\n",
    "ptRightBottom = (550, 550)\n",
    "point_color = (0, 255, 0) # BGR\n",
    "thickness = 2\n",
    "lineType = 4\n",
    "start_switch = False\n",
    "cap = cv2.VideoCapture(0)\n",
    "def predict(x):\n",
    "    x = cv2.cvtColor(x,cv2.COLOR_BGR2RGB)\n",
    "    x = tf.image.resize(x,[224,224])\n",
    "    x = 2 * tf.cast(x,dtype=tf.float32)/255. - 1\n",
    "    x = tf.reshape(x,(1,224,224,3))\n",
    "    out = newnet(x)\n",
    "    out = tf.argmax(out,axis=-1).numpy()[0]\n",
    "    # pred = labels[out]\n",
    "    # print(out)\n",
    "    return out\n",
    "\n",
    "while True:\n",
    "    ret,frame = cap.read()\n",
    "    frame = cv2.resize(frame,(1680,1000))\n",
    "    windows = frame[ptLeftTop[1]:ptRightBottom[1], ptLeftTop[0]:ptRightBottom[0]]\n",
    "    cv2.imshow(\"cap_windows\",windows)\n",
    "    cv2.rectangle(frame, ptLeftTop, ptRightBottom, point_color, thickness, lineType)\n",
    "    cv2.imshow(\"original\",frame)\n",
    "    if cv2.waitKey(1) & 0xFF is ord(\"s\"):\n",
    "        start_switch = True\n",
    "    if start_switch:\n",
    "        # if cv2.waitKey(1) & 0xFF is ord(\"r\"):\n",
    "        pred = predict(windows)\n",
    "        pred_result = labels_path[pred+1]\n",
    "        title = \"%s\"%(labels[pred+1])\n",
    "        img = cv2.imread(pred_result)\n",
    "        img = cv2.resize(img,(350,350))\n",
    "        cv2.putText(img, title, (30, 30), cv2.FONT_HERSHEY_PLAIN, 3.0, (0, 0, 255), 3)\n",
    "        cv2.imshow(\"predicted_result\",img)\n",
    "    else:\n",
    "        img = cv2.imread(start)\n",
    "        img = cv2.resize(img,(200,200))\n",
    "        cv2.imshow(\"switch\",img)\n",
    "    if cv2.waitKey(1) & 0xFF is ord('q'):\n",
    "        break\n",
    "cap.release()\n",
    "cv2.destroyAllWindows()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}